package com.edu.ahm.syn;


// 不安全的取钱
// 两个人去银行取钱
public class UnSafeBank {

    public static void main(String[] args) {
        Account account = new Account(1000,"61252213234ahm");

        Bank bank1 = new Bank(account, 800, "太原分行");
        Bank bank2 = new Bank(account, 800, "吕梁分行");

        bank1.start();
        bank2.start();
    }



}

class Account{
    int money; // 余额
    String accountNum; // 卡号

    public Account(int money,String accountNum){
        this.money = money;
        this.accountNum = accountNum;
    }
}
// 银行模拟取款
class  Bank extends Thread{
    Account account;
    int getMoney;

    public Bank(Account account,int getMoney, String name){
        super(name);
        this.account = account;
        this.getMoney = getMoney;
    }
    @Override
    public void run() {
//        super.run();
        // 判断如果取得钱不够了
        if (getMoney>account.money){
            System.out.println(Thread.currentThread().getName()+"钱不够，去不了");
            return;
        }
        try {
            // 模拟办理延时
            this.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        account.money -=getMoney;
        System.out.println(account.accountNum+"的余额为:"+account.money);


    }

}